package top.btcmarket;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.text.SimpleDateFormat;
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 * @author hexj
 * @date 2018/3/309:39
 */
public class GetUTCTimeUtil {

    private static String patten = "yyyy-MM-dd HH:mm:ss";

    private static final String DATE_PATTERN = "yyyy-MM-dd";

    /**
     * 得到UTC时间，类型为字符串，格式为"yyyy-MM-dd HH:mm:ss"<br />
     *
     * @return
     */
    public static String getUTCTimeStr() {
        return DateFormatUtils.formatUTC(new Date(), patten);
    }

    /**
     * 获取UTC日期，类型为字符串。格式为：yyyy-MM-dd
     *
     * @return
     */
    public static String getUTCDateStr() {
        return ZonedDateTime.now(Clock.systemUTC()).toLocalDate().format(DateTimeFormatter.ofPattern(DATE_PATTERN));
    }

    /**
     * 获取两个时间相差的小时数
     * @return
     */
    public static long getBetweenHours(String before,String after){
        DateTimeFormatter df = DateTimeFormatter.ofPattern(patten);
        LocalDateTime beforeTime = LocalDateTime.parse(before, df);
        LocalDateTime afterTime = LocalDateTime.parse(after, df);
        return ChronoUnit.HOURS.between(beforeTime, afterTime);
    }

    /**
     * 获取到当天的结束时间剩余的秒数
     * @return
     */
    public static Long getEndTime(){
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        Calendar c = Calendar.getInstance();
        long nowMills = c.getTimeInMillis();

        return (todayEnd.getTimeInMillis()-nowMills)/1000;
    }
    
    public static String getPreDate(String singTime) {
		// TODO Auto-generated method stub
		String preTime = singTime ;
		if(StringUtils.isNotBlank(singTime)){
			SimpleDateFormat sj = new SimpleDateFormat("yyyy-MM-dd");
	        Date d;
			try {
				d = sj.parse(singTime);
				Calendar calendar = Calendar.getInstance();
		        calendar.setTime(d);
		        calendar.add(Calendar.DATE, -1);
		        preTime = sj.format(calendar.getTime());
			} catch (java.text.ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       
		}
		
		return preTime;
	}
    
    /**
     * 获取当前时间
     * @return
     */
	public static Date getUtcTime() {
		String utcTimeStr = GetUTCTimeUtil.getUTCTimeStr();
		SimpleDateFormat sdf = new SimpleDateFormat(patten);
		try {
			return sdf.parse(utcTimeStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void main(String[] args) {
		Date utcTime = getUtcTime();
		
		System.out.println((utcTime.getTime()));
		System.out.println((utcTime.getTime()-1528842615900L));
		System.out.println((utcTime.getTime()-1528842615900L)/(1000*60));
		
	}
}
